
package gamepack;
import java.io.*;
//import org.codehaus.janino.util.resource;
import org.codehaus.janino.util.resource.ResourceFinder;

public class CustomResourceFinder extends org.codehaus.janino.util.resource.ResourceFinder {

public CustomResourceFinder() {
	System.out.println("CustomResourceFinder Invoked --------------------------------");
} 
																	//Eg. FullyQualifiedClassName= tankattack.sprites.Tank
public org.codehaus.janino.util.resource.Resource findResource(String filename) { 
	//	if( fullyQualifiedClassName == null ) {
	//		throw new RuntimeException("CustomResourceFinder.findResource(null string) invoked.");
	//	}
		//System.out.println("fullyQualifiedClassName=("+fullyQualifiedClassName+")");
	//	String javaFileName = fullyQualifiedClassName.replace('.' , '/') + ".java";  // javaFileName = tankattack/sprites/Tank.java		
	//	System.out.println("In CustomResourceFinder.findResource("+filename+")  Will now open it internally and check **" );
		org.codehaus.janino.util.resource.Resource res = new CompilerResource( filename );
		// Let us not return a resource unless we are capable of handling it. 
		InputStream is = null;
		try {
			is = res.open();
		}
		catch( IOException e) { 
		//System.out.println("Internal Check CustomResourceFinder.findResource("+filename+").open() threw IOException. So return null resfinder");
			return null;
		} 		
		if ( is == null ) {
		//System.out.println("Internal Check CustomResourceFinder.findResource("+filename+").open() returned null. So returning null resfinder");
			return null;
		} else {
		//System.out.println("Internal Check CustomResourceFinder.findResource("+filename+").open() returned something. So return resfinder");
			try { is.close(); } catch (IOException ex) { }						
		}
		return res;				
}

//public InputStream findResourceAsStream(String resourceName) throws IOException{
  //        return null;
//}

}

class CompilerResource implements org.codehaus.janino.util.resource.Resource {
	
	String filename;

	public CompilerResource(String filename_) {
		filename = filename_;
	}
	
	 public String getFileName() { 
			return filename;
	 }
 
	 // Returns the time of the last modification, in milliseconds since 1970, or 0L if the time cannot be determined. 
	 public long lastModified() {  
		return 0L;
	 }
	public java.io.InputStream open() throws IOException {
		//if( filename.equals("tankattack/sprites/Sprite.java" ) ) {
		//	return Application.getResourceAsStream("gamepack/Sprite.class");
		//}
		//System.out.println(" About to open inputstream : "+ filename );
		
		java.io.InputStream is = Application.getResourceAsStream(filename);
		if( is == null ) {
			//System.out.println("Please check if \""+ filename + "\" exists. Application.getResourceAsSream("+filename+") returned null Inputstream.");
		}
		return is;
	}
}


